//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "GaussianCopula.h"
using namespace Cephei::QL::Math::Copulas;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Copulas::CGaussianCopula::CGaussianCopula (Double rho) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phGaussianCopula = NULL;
#endif
        QuantLib::Real _rho = (QuantLib::Real)ValueHelper::Convert (rho); //d
        _ppGaussianCopula = new boost::shared_ptr<QuantLib::GaussianCopula> (new QuantLib::GaussianCopula ( _rho ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Copulas::CGaussianCopula::CGaussianCopula (boost::shared_ptr<QuantLib::GaussianCopula>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGaussianCopula = NULL;
#endif
	_ppGaussianCopula = &childNative;
    
}
Cephei::QL::Math::Copulas::CGaussianCopula::CGaussianCopula (QuantLib::GaussianCopula& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phGaussianCopula = NULL;
#endif
	_ppGaussianCopula = new boost::shared_ptr<QuantLib::GaussianCopula> (&childNative);
    
    _GaussianCopulaOwner = owner;
    
}

Cephei::QL::Math::Copulas::CGaussianCopula::CGaussianCopula (CGaussianCopula^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGaussianCopula = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppGaussianCopula = new boost::shared_ptr<QuantLib::GaussianCopula> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Copulas::CGaussianCopula::CGaussianCopula (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGaussianCopula = NULL;
#endif
	if (!t->IsSubclassOf(CGaussianCopula::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Copulas::CGaussianCopula::CGaussianCopula (QuantLib::Handle<QuantLib::GaussianCopula>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phGaussianCopula = &childNative;
	_ppGaussianCopula = &static_cast<boost::shared_ptr<QuantLib::GaussianCopula>>(childNative.currentLink());
    
    _GaussianCopulaOwner = owner;
}
Cephei::QL::Math::Copulas::CGaussianCopula::CGaussianCopula (QuantLib::Handle<QuantLib::GaussianCopula> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phGaussianCopula = &childNative;
	_ppGaussianCopula = &static_cast<boost::shared_ptr<QuantLib::GaussianCopula>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Copulas::CGaussianCopula::CGaussianCopula (QuantLib::GaussianCopula childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGaussianCopula = NULL;
#endif
	_ppGaussianCopula = new boost::shared_ptr<QuantLib::GaussianCopula> (new QuantLib::GaussianCopula (childNative));
    
}
#endif

Cephei::QL::Math::Copulas::CGaussianCopula::~CGaussianCopula ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppGaussianCopula != NULL)
    {
	    delete _ppGaussianCopula;
        _ppGaussianCopula = NULL;
    }
}
Cephei::QL::Math::Copulas::CGaussianCopula::!CGaussianCopula ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppGaussianCopula != NULL)
    {
	    delete _ppGaussianCopula;
    }
}
QuantLib::GaussianCopula& Cephei::QL::Math::Copulas::CGaussianCopula::GetReference ()
{
    if (_ppGaussianCopula == NULL) throw REFNEW NativeNullException ();
	return **_ppGaussianCopula;
}
boost::shared_ptr<QuantLib::GaussianCopula>& Cephei::QL::Math::Copulas::CGaussianCopula::GetShared ()
{
    if (_ppGaussianCopula == NULL) throw REFNEW NativeNullException ();
	return *_ppGaussianCopula;
}
QuantLib::GaussianCopula* Cephei::QL::Math::Copulas::CGaussianCopula::GetPointer ()
{
    if (_ppGaussianCopula == NULL) throw REFNEW NativeNullException ();
	return &**_ppGaussianCopula;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::GaussianCopula>& Cephei::QL::Math::Copulas::CGaussianCopula::GetHandle ()
{
	if (_phGaussianCopula == NULL)
	{
		_phGaussianCopula = new Handle<QuantLib::GaussianCopula> (*_ppGaussianCopula);
	}
	return *_phGaussianCopula;
}
#endif
bool Cephei::QL::Math::Copulas::CGaussianCopula::HasNative () 
{
	return (_ppGaussianCopula != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Copulas::IGaussianCopula^ Cephei::QL::Math::Copulas::CGaussianCopula_Factory::Create (Double rho)
{
    return REFNEW CGaussianCopula ( rho);
}
